A financial calculation
While formulas provide a powerful way to display calculations on View pages, they are not so useful on Edit pages, since formulas cannot dynamically use changes made by a user while entering data into a form. However, formulas can be used on Edit and New pages as placeholders for dynamically calculated values, even though the actual calculations now have to be performed on the client side using HTML events and the JavaScript API.
Consider the example of an object that has three fields:
- A Currency field named Amount with the integration name amount
- A Percent field named Rate with the integration name rate
- A Formula field (with a return type of Currency) named
Interest with the integration name interest and
the formula body
{!amount}*{!rate}/100
The View page looks like this:
You can add these same fields to the Edit page:
However, when the user changes the content of the two editable fields (Rate and Amount), the content of the formula field does not change. To have the field change, you need to add a financial calculation similar to the following:
- Add a script component to the Edit and New pages (see Editing pages):
function cust_interest() { var m = rbf_getFloat(rbf_getFieldValue("amount")); var r = rbf_getFloat(rbf_getFieldValue("rate")); var interest = m*r/100; rbf_setFieldContent("interest", rbf_formatCurrency(interest)); } - Add onchange handlers to the amount and rate fields:
if (typeof cust_interest == 'function') cust_interest();
- Add the same code to the onload script on the Edit page for consistency.
Now the content of the Interest field will be dynamically updated on the Edit and New pages in a consistent manner with the View page.